<?php
namespace App\Admin\Controllers\FileModel;
use App\Http\Controllers\Controller;
use Dcat\Admin\FormStep\Form as StepForm;
use App\Models\InputStepForm;
use App\Models\InformationList;
use Dcat\Admin\Form;
use Dcat\Admin\Admin;
use Dcat\Admin\Layout\Content;
use Illuminate\Http\Request;
use PhpOffice\PhpWord\TemplateProcessor;
use PhpOffice\PhpWord\PhpWord;
use Dcat\Admin\Grid;
use Illuminate\Support\Facades\DB;
use Log;
// use Dcat\Admin\Widgets\Form;
class AfterSalesServiceController extends Controller
{
    //售后服务确认单
    protected $options = [
        1 => '显示文本框',
        2 => '显示编辑器',
        3 => '显示文件上传',
        4 => '还是显示文本框',
    ];
    // use PreviewCode;
    public function index(Content $content)
    {
        return $content
            // ->body('<div style="margin:5px 0 15px;">'.$this->buildPreviewButton().'</div>')
            ->body($this->form())
            ->header('售后服务确认单')
            ->description('分步表单');
    }
    protected function form()
    {
        Admin::js('/stepform/js/jquery.smartWizard.min.js');
        Admin::css('/stepform/css/step.css');
        return Form::make(new InputStepForm(), function (Form $form) {
            $form->title('售后服务确认单');
            $form->action('AfterSalesServicestep');
            $form->disableListButton();
            $form->multipleSteps()
                ->remember() 
                ->padding('30px 18px 30px')
                ->add('性能试验表', function (StepForm $step) use ($form) {
                    $step->text('projectname',trans('项目名称'))->required();
                    $step->text('pa1',trans('产品规格'))->required();
                    $step->radio('check1', '阶段')->options(['1'=>'调试', '2'=>'质保内', '3'=>'质保外'])->default(1);
                    $step->text('pa2',trans('编号'))->required();
                    $step->datetime('datetime', '维护时间')->required();
                    $step->textarea('pa5',trans('故障现象'))->required();
                    $step->textarea('pa6',trans('解决方法'))->required();
                    $step->textarea('pa7',trans('更换的配件'))->required();
                    $step->radio('check2', '拆下故障元器件处理措施')->options(['1'=>'寄回公司', '2'=>'就地遗弃', '3'=>'其他'])
                    ->when('3',function(StepForm $step){
                        $step->text('pa8','处理措施');
                    })->default(1);
                });
        });
    }

    public function step(Request $request){
        $info = $request->all();
        if(empty($info['CURRENT_VALIDATION_STEP'])&&$info['ALL_STEPS']==1){//提交
            Log::info($info);
            //实例化 phpword 类
            $PHPWord = new PhpWord();
            //指定事先制作好的模板文件路径
            $templateProcessor = new TemplateProcessor(public_path("filemodel/after_sales_service.docx"));

            $templateProcessor->setValue('projectname', $info['projectname']);

            $true = '<w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:sym w:font="Wingdings" w:char="00FE"/></w:r>';
            $false = '<w:r><w:rPr><w:rFonts w:hint="eastAsia"/><w:lang w:val="en-US" w:eastAsia="zh-CN"/></w:rPr><w:sym w:font="Wingdings" w:char="00A8"/></w:r>';
            $ck1 = $info['check1']==1?$true:$false;
            $ck2 = $info['check1']==2?$true:$false;
            $ck3 = $info['check1']==3?$true:$false;
            $templateProcessor->setValue('ck1', $ck1);
            $templateProcessor->setValue('ck2', $ck2);
            $templateProcessor->setValue('ck3', $ck3);
            $ck4 = $info['check2']==1?$true:$false;
            $ck5 = $info['check2']==2?$true:$false;
            $ck6 = $info['check2']==3?$true:$false;
            $templateProcessor->setValue('ck4', $ck4);
            $templateProcessor->setValue('ck5', $ck5);
            $templateProcessor->setValue('ck6', $ck6);

            $templateProcessor->setValue('pa1', $info['pa1']);
            $templateProcessor->setValue('pa2', $info['pa2']);
            $templateProcessor->setValue('pa3', $info['datetime']);
            $templateProcessor->setValue('pa5', $info['pa5']);
            $templateProcessor->setValue('pa6', $info['pa6']);
            $templateProcessor->setValue('pa7', $info['pa7']);
            $templateProcessor->setValue('pa8', $info['pa8']?:'');
            
            unset($info['_file_']);
            unset($info['CURRENT_VALIDATION_STEP']);
            unset($info['ALL_STEPS']);
            unset($info['_token']);
            $data['admin_id'] = Admin::user()->id;
            $data['name'] = $info['projectname'].date('Y-m-d H:i');
            $data['data'] = $info;
            // $data['content'] = json_encode($info);
            // $insertData['content'] = json_encode($data);
            $insertData['name'] = $data['name'];
            $insertData['type'] = 7;
            $insertData['admin_id'] = Admin::user()->id;
            $insertData['created_at'] = date('Y-m-d H:i:s');
            $templateProcessor->saveAs(public_path('filemodellist/'.$data['name'].".docx"));
            InformationList::insert($insertData);
            return $this->form()->saving(function (Form $form) {
                // 清空缓存
                $form->multipleSteps()->flushStash();
                // 拦截保存操作
                return response(
                    $form->multipleSteps()
                        ->done()
                        ->render()
                );
            })->store();
        }
    }
    public function list($id,Content $content){
        return $content
            ->header('表格列表')
            ->description()
            ->body($this->grid($id));
    }
    protected function grid($typeid)
    {
        return new Grid(InformationList::where(['admin_id'=>Admin::user()->id,'type'=>$typeid]), function (Grid $grid) {
            $grid->showActions();
            $grid->disableDeleteButton();
            $grid->disableQuickEditButton();
            $grid->disableViewButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->fixColumns(2);
            $grid->column('id', 'ID');
            $grid->column('name', '名称');
            $grid->column('type','类型')->display(function($type) {
                return Db::table('xfy_info_model')->where(['id'=>$type])->value('name');;
            });
            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $id = $actions->getKey();
                $edit_url = admin_base_path('downloadfile/'.$id);
                $actions->append("<a href='$edit_url'>下载</a>");
            });
        });
    }
    /**
     * 保存
     *
     * @return \Symfony\Component\HttpFoundation\Response
     */
    public function store(Request $requset)
    {
        return $this->form()->saving(function (Form $form) {
            
            // 清空缓存
            $form->multipleSteps()->flushStash();
            // 拦截保存操作
            return response(
                $form->multipleSteps()
                    ->done()
                    ->render()
            );
        })->store();
    }
}